package com.dbpms.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dbpms.mapper.ChamberMapper;
import com.dbpms.pojo.*;
import com.dbpms.service.IChamberService;
import com.dbpms.service.IStudentManageService;
import com.dbpms.service.ITransferService;
import com.dbpms.until.Constants;
import com.dbpms.until.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/transfer")
public class TransferChamberController {
    @Autowired
    private IStudentManageService studentManageService;
    @Autowired
    private ITransferService transferService;
    @Autowired
    private IChamberService chamberService;
    @Resource
    private ChamberMapper chamberMapper;
    /**
     * 前往申请报告页面
     * @return
     */
    @GetMapping("/toTransferApply")
    public String totoTransferApply(HttpServletRequest req, Model model) {

        User user = (User) req.getSession().getAttribute(Constants.USER_SESSION);
        int student_id = user.getUserStudentId();
        // 根据学生id查找学生信息
        StudentInfo studentInfo = studentManageService.queryOneStuInfo(student_id);
        // 寝室楼/楼层/寝室号
        QueryWrapper<ChamberInfo> wrapper = new QueryWrapper<>();
            wrapper.eq("ci.chamber_id",studentInfo.getStudentChamberId()); // 学生寝室id
        ChamberInfo chamberInfo = chamberMapper.queryAllChtInfo(null,wrapper).get(0);
        // 获取寝室楼信息
        List<Chamberfloorinfo> chamberfloorinfos = chamberService.queryAllChtFloorInfo();


        model.addAttribute("chamber_floor_number_builder",chamberInfo.getChamberFloorName()+" / "+ chamberInfo.getChamberFloor() +" / "+chamberInfo.getChamberHotelNumber());
        model.addAttribute("stu_info",studentInfo);
        model.addAttribute("chamberfloorinfos",chamberfloorinfos);

        // 保存调寝单需要的补充信息
        model.addAttribute("ChamberId",chamberInfo.getChamberId());
        model.addAttribute("ChamberFloorId",chamberInfo.getChamberFloorId());
        model.addAttribute("ChamberFloor",chamberInfo.getChamberFloor());

        return "chamber/sleepAdjust/addAdjustmentApply";
    }

    /**
     * 根据寝室楼和楼层查找对应寝室
     * @param Chamber_floor_number : 楼层
     * @param Chamber_floor_id ： 寝室楼id
     * @return
     */
    @GetMapping("/ByFloorNumberSelectChamber")
    @ResponseBody
    public Result ByFloorNumberSelectChamber(@RequestParam("Chamber_floor_number") int Chamber_floor_number,
                                             @RequestParam("Chamber_floor_id") int Chamber_floor_id) {
        List<ChamberInfo> infos = chamberService.queryApplyChtInfo(Chamber_floor_id,Chamber_floor_number);
        return Result.success(infos);
    }

    @PostMapping("/addTransferInfo")
    public String addTransferInfo(HttpServletRequest req,TransferChamberInfo info,Model model) {
        info.setApplyChamberSchedule("申请中");
        User user = (User) req.getSession().getAttribute(Constants.USER_SESSION);
        int student_id = user.getUserStudentId();

        if (info.getApplyChamberId() == info.getApplyNewChamberId()){
            model.addAttribute(Constants.MESSAGE,"申请调寝不允许为原寝室，请重新选择");
        }else {
            boolean pd = transferService.addTransferInfo(info,student_id);
            if (pd){
                model.addAttribute(Constants.MESSAGE,"申请成功");
            }else{
                model.addAttribute(Constants.MESSAGE,"申请失败，已有调寝报告在申请中，请结束后再重新提交报告...");
            }
        }

        return "chamber/sleepAdjust/addAdjustmentApply";
    }

    /**
     * 查看该学生的申请进度
     * @return
     */
    @GetMapping("/showStuApplyTransferInfo")
    public String showStuApplyTransferInfo(HttpServletRequest req,Model model) {
        User user = (User) req.getSession().getAttribute(Constants.USER_SESSION);
        int student_id = user.getUserStudentId();
        List<TransferChamberInfo> infos = transferService.queryStuApplyTransferInfo(student_id);

        model.addAttribute("TransferInfos",infos);
        return "chamber/sleepAdjust/stu_showAdjustmentApply";
    }
}
